<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">

            <div class="search-item">
                <label>名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="请输入"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>热词类型：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.type"
                        filterable
                        clearable
                        placeholder="类型"
                        class="address-select"
                    >
                        <el-option
                            v-for="item in hotWordTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>

            <div class="search-item">
                <label>起止时间：</label>
                <el-date-picker
                    v-model="queryData.startTime"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
            </div>
            <div class="search-item">
                <label class="minWidthClear">至：</label>
                <el-date-picker
                    v-model="queryData.endTime"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
            </div>
            <div class="search-item">
                <label>热词来源：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.source"
                        filterable
                        clearable
                        placeholder="来源"
                        class="address-select"
                    >
                        <el-option
                            v-for="item in hotWordSource"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="search-item">
                <div class="table-query-input">
                    <el-checkbox v-model="queryData.enable">查看当前搜索热词</el-checkbox>
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>

        <div class="table-operation-box">

            <el-button
                type="primary"
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickAdd('新增热词')"
            >添加热词
            </el-button>
            <el-button
                type="danger"
                style="margin-bottom: 2em"
                icon="el-icon-delete"
                :disabled="selectDisable"
                @click="
                    tableDelete(
                        '/user/sys/hot-word/deleted-list',
                        {
                            ids: tableSelect
                        },
                        true
                    )
                "
            >批量删除
            </el-button>
        </div>

        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                :row-class-name="tableRowClassName"
                @selection-change="tableSelectionChange"
                @cell-click="tabClick"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <!--                <el-table-column-->
                <!--                    prop="createdByName"-->
                <!--                    label="创建者"-->
                <!--                    width="120"-->
                <!--                ></el-table-column>-->
                <el-table-column prop="type" label="类型" width="120">
                    <template slot-scope="scope">
                        <k-tag
                            :tag="hotWordType[scope.row.type]"
                            :text="true"
                        ></k-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="热词名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="source"
                    label="热词来源"
                    min-width="100"
                >
                    <template slot-scope="scope">
                        <k-tag
                            :tag="scope.source===0?'用户搜索':'后台设置'"
                            :text="true"
                        ></k-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="sort" label="排序" width="120">
                    <template slot-scope="scope">
                        <span
                            v-if="
                                scope.row.index === tabClickIndex &&
                                    tabClickLabel === '排序'
                            "
                        >
                            <el-input
                                v-model="scope.row.sort"
                                type="number"
                                maxlength="20"
                                placeholder="排序"
                                size="mini"
                                @blur="inputBlur(scope.row)"
                            />
                        </span>
                        <span v-else>{{ scope.row.sort }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="创建时间"
                    min-width="100"
                >
                </el-table-column>
                <el-table-column
                    prop="remark"
                    label="备注"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.enable"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"

                            :loading="scope.row.opLoading"
                            @click="setHot1(scope.row)"
                        >设为搜索热词
                        </el-button>
                        <el-button
                            v-if="scope.row.enable"
                            lass="primary"
                            type="text"
                            size="mini"
                            class="warning"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="cancelHot(scope.row)"
                        >取消搜索热词
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="clickSimpleEdit(scope.row, '编辑')"
                        >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            type="text"
                            size="mini"
                            class="danger"
                            icon="el-icon-delete"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/user/sys/hot-word/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>

        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            :before-close="dialogClose"
            custom-class="one-dialog"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from"
            >
                <el-form-item label="热词类型" prop="type">
                    <el-select v-model="dataForm.type" placeholder="请选择">
                        <el-option
                            v-for="item in hotWordTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="dataForm.sort"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-checkbox v-model="dataForm.enable">设为热词</el-checkbox>
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="dataForm.remark"
                        type="textarea"
                        :rows="4"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="formDataSave('/user/sys/hot-word/update',cover)"
                >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {updateHotword} from "@/api/user";
import {put} from "@/api/request";

export default {
    name: "HotwordList",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            tabClickIndex: null,
            tabClickLabel: "",
            hotWordTypeList: [
                {label: "试题热词", value: "question"},
                {label: "试卷热词", value: "paper"},
                {label: "资料资讯", value: "resource"},
            ],
            hotWordSource: [
                {label: "用户搜索", value: 0},
                {label: "后台设置", value: 1}
            ],
            queryData: {
                name: "",
                code: "",
                enable: null,
                startTime: null,
                endTime: null,
                type: "",
                orders: [
                    {
                        asc: false,
                        column: "sort"
                    }
                ]
            },
            initAddForm: {
                name: null,
                code: null,
                url: null,
                enable:null
            },
            dataForm: {},
            dataFormRule: {
                name: [
                    {
                        required: true,
                        message: "请输入名称",
                        trigger: "blur"
                    }
                ],
                sort: [
                    {
                        required: true,
                        message: "请输入排序",
                        trigger: "blur"
                    }
                ],
                type: [
                    {
                        required: true,
                        message: "请选择类型",
                        trigger: "change"
                    }
                ]
            }
        };
    },
    computed: {
        ...mapState({
            hotWordType: state => state.status.hotWordType
        })
    },
    watch: {},
    created() {
    },
    mounted() {
    },
    methods: {
        search(queryData) {

            this.loadTable("/user/sys/hot-word/page", queryData);
        },
        cover() {
            this.dataForm.source = 1;
            if (this.dataForm.enable === undefined) {
                this.dataForm.enable = false;
            }
            return this.dataForm
        },
        tableRowClassName({row, rowIndex}) {
            // 把每一行的索引放进row
            row.index = rowIndex;
        },
        tabClick(row, column, cell, event) {
            switch (column.label) {
            case "排序":
                this.tabClickIndex = row.index;
                this.tabClickLabel = column.label;
                break;
            default:
                return;
            }
        },
        inputBlur(row) {
            this.tabClickIndex = null;
            this.tabClickLabel = "";
            updateHotword({
                id: row.id,
                sort: row.sort
            });
        },
        setHot1(data) {
            data.enable = true;
            put("/user/sys/hot-word/update", data).then(res => {
                this.search(this.queryData)
            })
        },
        cancelHot(data) {
            data.enable = false;
            put("/user/sys/hot-word/update", data).then(res => {
                this.search(this.queryData)
            })
        },
    }
};
</script>

<style lang="scss" scoped>
.school-logo {
    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 108px;
        height: 108px;
        line-height: 108px;
        text-align: center;
    }

    .logo-img {
        width: 108px;
        height: 108px;
        display: block;
    }
}

.table-edit-from {
    .address-select {
        width: 8em;
        margin-right: 2em;
    }
}
</style>
<style>
.school-logo .logo-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.minWidthClear {
    min-width: 0 !important;
}

.school-logo .logo-uploader .el-upload:hover {
    border-color: #409eff;
}
</style>
